package com.company.project.modular.caigou.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.company.project.core.base.controller.BaseController;
import com.company.project.core.base.tips.ErrorTip;
import com.company.project.core.log.LogObjectHolder;
import com.company.project.modular.system.model.Standard;
import com.company.project.modular.system.service.*;
import com.company.project.modular.system.model.Audit;
import com.company.project.modular.system.model.Purchase;
import com.company.project.modular.system.warpper.AuditWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 采购单管理控制器
 *
 * @author fengshuonan
 * @Date 2018-10-30 15:28:04
 */
@Controller
@RequestMapping("/purchase")
public class PurchaseController extends BaseController {

    private String PREFIX = "/caigou/purchase/";

    @Reference
    private IPurchaseService purchaseService;
    @Reference
    private IAuditService auditService;
    @Reference
    private IBuyService buyService;
    @Reference
    private ISupplierService supplierService;


    /**
     * 跳转到采购单管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "purchase.html";
    }

    /**
     * 跳转到添加采购单管理
     */
    @RequestMapping("/purchase_add")
    public String purchaseAdd() {
        return PREFIX + "purchase_add.html";
    }

    /**
     * 跳转到修改采购单管理
     */
    @RequestMapping("/purchase_update/{purchaseId}")
    public String purchaseUpdate(@PathVariable Integer purchaseId, Model model) {
        Purchase purchase = purchaseService.selectById(purchaseId);
        model.addAttribute("item", purchase);
        LogObjectHolder.me().set(purchase);
        return PREFIX + "purchase_edit.html";
    }

    /**
     * 跳转到修改采购单管理
     */
    @RequestMapping("/purchase_state/{purchaseId}")
    public String purchaseState(@PathVariable Integer purchaseId, Model model) {
        Purchase purchase = purchaseService.selectById(purchaseId);
        model.addAttribute("item", purchase);
        LogObjectHolder.me().set(purchase);
        return PREFIX + "purchase_state.html";
    }

    /**
     * 获取报买名称
     */
    @RequestMapping("/purName")
    @ResponseBody
    public List<String> purName(Model model) {
        /*查询报买表里报买名称*/
        List<String> purName = buyService.findByBuyName();
        System.out.println("purName"+purName);
        return purName;
    }

    /**
     * 报买表里已经通过审核的报买名称
     */
    @RequestMapping("/purSupplier")
    @ResponseBody
    public List<String> purSupplier(Model model) {
        /*查询审核表里面已经通过了审核的报买单号*/
        List<String> purSupplier = supplierService.findSupplierName();
        return purSupplier;
    }
    /**
     * 获取采购单管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> purchase = purchaseService.list(condition);
        return super.warpObject(new AuditWarpper(purchase));
    }

    /**
     * 新增采购单管理，添加采购单的同时新增审核单
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Purchase purchase, Audit audit, String purName, BindingResult bindingResult) {
        //从界面上获取采购名称，采购名称必须存在于报买表的报买名称里面，若不存在则出现警告
        //做法是将purName的值直接传给后台，看是否为空，不为空则是存在。
         /* List<Map<String,Object>> buy = buyService.findByName(purName);
       if(buy.size()==0){
            System.out.println("您输入的对照品不存在");
            return new ErrorTip(0,"您输入的对照品不存在，请参考报买表重新输入");
        }else{
            System.out.println("-------------------------------------");
            }*/

        String maxNumber = String.valueOf(purchaseService.findByMaxNumber().get(0).getPurId());
        if ("null".equals(maxNumber)) {
            maxNumber = null;
            int a = 20001;
            String number = String.valueOf(a);
            purchase.setPurId(number);
        } else {
            int a = Integer.valueOf(maxNumber) + 1;
            String number = String.valueOf(a);
            purchase.setPurId(number);
        }
        String supplier = purchase.getPurSupplier();
        String purSupplierTel = supplierService.findSupplierTel(supplier);
        purchase.setPurSupplierTel(purSupplierTel);
        purchase.setPurState(1);//设置回购状态为未回购
        purchaseService.insert(purchase);

        /*新增审核单*/
        String maxNumberAudit = String.valueOf(auditService.findByMaxNumber().get(0).getAuditId());
        if ("null".equals(maxNumberAudit)) {
            maxNumberAudit = null;
            int a = 30001;
            String number = String.valueOf(a);
            audit.setAuditId(number);
        } else {
            int a = Integer.valueOf(maxNumberAudit) + 1;
            String number = String.valueOf(a);
            audit.setAuditId(number);
        }
        audit.setAuditName(purName);
        /*将purId存到auditPurId里面*/
        if ("null".equals(maxNumber)) {
            audit.setAuditPurId("20001");
        } else {
            int a = Integer.valueOf(maxNumber) + 1;
            String number = String.valueOf(a);
            audit.setAuditPurId(number);
        }
        /*审核人==登录系统的人的名称*/
        super.getSession().getAttribute("username");//admin
        Object name = super.getSession().getAttribute("name");  //张三
        String persion = String.valueOf(name);
        audit.setAuditPersion(persion);
        audit.setAuditState(2);
        audit.setAuditPass(3);
        audit.setAuditPersion("等待审核");
        auditService.insert(audit);
        return new ErrorTip(200, "添加成功00");
        /*return SUCCESS_TIP;*/

    }

    /**
     * 删除采购单管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer purchaseId) {
        purchaseService.deleteById(purchaseId);
        return SUCCESS_TIP;
    }

    /**
     * 修改采购单管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Purchase purchase,BindingResult bindingResult) {
        purchaseService.updateById(purchase);
        return SUCCESS_TIP;
    }
    /**
     * 修改采购单管理
     */
    @RequestMapping(value = "/state")
    @ResponseBody
    public Object state(Purchase purchase,BindingResult bindingResult) {
        //得到long类型当前时间
        long l = System.currentTimeMillis(); //new日期对象
        Date date = new Date(l);
        /*SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");*///转换提日期输出格式
        purchase.setPurDate(date);
        System.out.println("purchase.getPurState()"+purchase.getPurState());
        purchaseService.updateById(purchase);
        return SUCCESS_TIP;
    }

    /**
     * 采购单管理详情
     */
    @RequestMapping(value = "/detail/{purchaseId}")
    @ResponseBody
    public Object detail(@PathVariable("purchaseId") Integer purchaseId) {
        return purchaseService.selectById(purchaseId);
    }
}
